<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;

class TeacherController extends BaseController {

	public function index() {
		$this->assign('teachers', D('Teacher')->relation('academy')->select());
		$this->display();
	}

	public function add() {
		$this->assign('academies', M('Academy')->select());
		$this->display();
	}

	public function addPost() {
		if(!IS_POST) {
			$this->redirect('Teacher/index');
		}
		$teacher = D('Teacher');
		if(!$teacher->create()) {
			$this->error($teacher->getError());
		}
		if(!$teacher->add()) {
			$this->error('数据写入失败！');
		}
		$this->success('添加成功！', U('Teacher/index'), 0);
	}

	public function addBatch() {
		$this->assign('academies', M('Academy')->select());
		$this->display();	
	}

	public function addBatchPost() {
		if(!IS_POST) {
			$this->redirect('Teacher/index');
		}
		$data['academy_id'] = I('post.academy_id');

		// 上传文件
		$upload = new \Think\Upload();
		$upload->rootPath = './Application/Runtime/Temp/';
		$upload->savePath = '';
		$upload->autoSub = false;
		$upload->exts = array('xls', 'xlsx');
		$result = $upload->upload();
		if(!$result) {
			$this->error($upload->getError());
		}

		// 读取文件
		$filename = $upload->rootPath . $result['file']['savepath'] . $result['file']['savename'];
		import('Org.Util.PHPExcel');
		import('Org.Util.PHPExcel.IOFactory');
		$objPHPExcel = \PHPExcel_IOFactory::load($filename);
		$sheet = $objPHPExcel->getSheet(0);
		$highestRowNumber = $sheet->getHighestRow();
		$teachers = array();
		for($i = 1; $i <= $highestRowNumber; $i++) {
			$cellAVal = $sheet->getCell('A' . $i)->getValue();
			$cellBVal = $sheet->getCell('B' . $i)->getValue();
			$teachers[] = array($cellAVal, $cellBVal);
		}
		
		// 读取文件后删除
		unlink($filename);

		// 验证数据
		$teacher = D('Teacher');
		foreach($teachers as $k => $row) {
			$data['id'] = $row[0];
			$data['name'] = $row[1];
			if(!$teacher->create($data, 1)) {
				$this->error('第' . ($k + 1) . '行数据有误：' . $teacher->getError(), U('Teacher/addBatch'));
			}
		}

		// 写入数据
		foreach($teachers as $k => $row) {
			$data['id'] = $row[0];
			$data['password'] = md5($row[0]);
			$data['name'] = $row[1];
			if(!$teacher->add($data)) {
				$this->error('数据写入失败');
			}
		}

		$this->success('导入成功！');
	}

	public function deletePost() {
		if(!IS_POST) {
			$this->redirect('Teacher/index');
		}
		$id = (int)I('post.id');
		$teacher = D('Teacher');
		if($teacher->hasChildObj($id)) {
			$this->error($teacher->getError());
		}
		if(M('Teacher')->where(array('id' => $id))->delete()) {
			$this->success('删除成功！', U('Teacher/index'), 0);
		} else {
			$this->error('删除失败！');
		}
	}

	public function modify() {
		$id = (int)I('id');
		$teacher = D('Teacher')->where(array('id' => $id))->relation('academy')->find();
		$this->assign('teacher', $teacher);
		$this->assign('academies', M('Academy')->select());
		$this->display();
	}

	public function modifyPost() {
		if(!IS_POST) {
			$this->redirect('Teacher/index');
		}
		$teacher = D('Teacher');
		if(!$teacher->create()) {
			$this->error($teacher->getError());
		}
		$result = $teacher->save();
		if(false === $result) {
			$this->error('数据更新失败！');
		}
		$this->success('修改成功！', U('Teacher/index'), 0);
	}

	public function detail() {
		$id = (int)I('id');
		$teacher = D('Teacher')->where(array('id' => $id))->relation('academy')->find();
		$this->assign('teacher', $teacher);
		$this->assign('academies', M('Academy')->select());
		$this->display();
	}

}
